﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Futurism.Universal.Foundation
{
    public abstract class BootStrapper : Application
    {
        private TypeContainer container = new TypeContainer();

        public RegionManager RegionManager { get; private set; }

        public BootStrapper()
        {
            RegionManager = new RegionManager(container);
            container.Register<RegionManager>(RegionManager);
            this.MainPage = new Page();
        }

        public void Run()
        {
            var mainPage = container.Resolve(GetShell()) as Page;
            this.MainPage = mainPage;
        }

        public void AddModule(Type type)
        {
            foreach (var exportedType in type.GetTypeInfo().Assembly.DefinedTypes.Where(t => t.GetCustomAttribute<ExportAttribute>() != null))
            {
                var exportAttribute = exportedType.GetCustomAttribute<ExportAttribute>();
                container.Register(exportedType.AsType(), exportAttribute.Shared, exportAttribute.ExportType);
            }
        }

        public abstract Type GetShell();
    }
}
